define(['./module'],function(goodscommentmodule){
	'use strict';


	/**
	 * 商品评论详情控制器
	 */
	goodscommentmodule.controller('goodscommentreplyment',ServerGoodsCommentReplyment);
	function ServerGoodsCommentReplyment($scope,$http,$location){
		$scope.commentid= $location.search().commentid;
		if($scope.commentid!=undefined){
			$http({
				method:'POST',
				url:'../mall/goodscomment/findAllReplyByCommentId',
				data:{
					'commentid':$scope.commentid
				}
			}).success(function(data){
				if(data.sucflag){
					$scope.title='评论--'+data.bean.title+'--详细信息';
					$scope.commentdetail=data.bean;//商品评论详情
					$scope.replys=data.beanlist;
					$scope.commentsize=data.beanlist.length;
				}

			}).error(function(data){
				$scope.errors = {
					show: true,
					msg: '系统异常'
				}
			});
		}
		/**
		 * 回复评论
		 */
		$scope.replyComment=function(){
			if($scope.commentcontent==undefined||$scope.commentcontent==""){
				$scope.errors={
					show:true,
					msg:"请输入回复内容"
				}
				return false;
			}
			$http({
				method:'POST',
				url:'../mall/goodscomment/reply',
				data:{
					'commentcontent':$scope.commentcontent,
					'goodsid':$scope.commentdetail.goodsid,
					'goodsname':$scope.commentdetail.goodsname,
					'commentid':$scope.commentid
				}
			}).success(function(data){
				if(data.sucflag){
					$scope.info={
						show:true,
						msg:'商品评论回复添加成功'
					}
					//$location.path('/goodscommentreplyment').search({'operate':'find','commentid':$scope.commentid});
				}

			}).error(function(data){
				$scope.errors = {
					show: true,
					msg: '系统异常'
				}
			});
		}

		/**
		 * 审核评论
		 */
		$scope.reviewComment=function(type){
			$http({
				method:'POST',
				url:'../mall/goodscomment/reviewCommentById',
				data:{
					'reviewed':type,
					'commentid':$scope.commentid
				}
			}).success(function(data){
				if(data.sucflag){
					$scope.info={
						show:true,
						msg:'审核成功'
					}
				}

			}).error(function(data){
				$scope.errors = {
					show: true,
					msg: '系统异常'
				}
			});
		}

	}


	/*=====End Of Page Element Directive=====*/
	
	/*=====Begin Of Save and Edit GoodsCommentT=====*/
	goodscommentmodule.controller('goodscomment',ServerSaveGoodsComment);
	function ServerSaveGoodsComment($scope,$http,$location){
		//通过location中的operate参数区分操作行为
		var operate=$location.search().operate;
		if(operate!=undefined&&operate=="save"){
			$scope.goodsid=$location.search().goodsid;
			$scope.goodsname=$location.search().goodsname;
			//控制保存按钮显示
			$scope.savebtn={show:true};
			$scope.title='添加商品--'+$scope.goodsname+'--的评论';
			$scope.status="1";

			//保存数据方法
			$scope.save=function(){
				if(validate()){
					var commentcontent=$scope.commentcontent;
					var title=$scope.commenttitle;
					var score=$scope.score;
					if(score==undefined){
						score=1;
					}
					var zan=$scope.zan;
					if(zan==undefined){
						zan=1;
					}
					var cai=$scope.cai;
					if(cai==undefined){
						cai=1;
					}
					var status=$scope.status;
					$http({
						method:'POST',
						url:'../mall/goodscomment/save',
						data:{
							'commentcontent':commentcontent,
							'goodsid':$scope.goodsid,
							'goodsname':$scope.goodsname,
							'title':title,
							'score':score,
							'zan':zan,
							'cai':cai,
							'status':status
						}
					}).
					success(function(data,status,headers,config){
						if(data.sucflag){
							$scope.info={
									show:true,
									msg:'商品评论信息添加成功'
							}
							$location.path('/goodscommentonement');
						}
					}).
					error(function(data,status,headers,config){
						$scope.errors={
								show:true,
								msg:'系统异常'
						}
					});
				}
			}
		}
		

		
		//表单字段验证
		function validate(){
			var commenttitle=$scope.commenttitle;
			if(commenttitle==undefined||commenttitle==""){
				$scope.errors={
						show:true,
						msg:"请输入标题"
				}
				return false;
			}
			var goodsname=$scope.goodsname;
			if(goodsname==undefined||goodsname==""){
				$scope.errors={
						show:true,
						msg:"请选择商品"
				}
				return false;
			}
			var commentcontent=$scope.commentcontent;
			if(commentcontent==undefined||commentcontent==""){
				$scope.errors={
						show:true,
						msg:"请输入评论内容"
				}
				return false;
			}
			return true;
		}
	}
	/*=====End Of Save and Edit GoodsCommentT=====*/
	
	/*=====Begin Of Find List GoodsCommentT=====*/
	goodscommentmodule.controller('goodscommentment',ServerGoodsCommentListCtrl);
	//查询列表数据
	function ServerGoodsCommentListCtrl($http,$location,$compile,$scope,$rootScope,$resource,DTOptionsBuilder,DTColumnBuilder,DTAjaxRenderer){
		$scope.title="商品评论列表";
		var vm=this;
		vm.message='';
		vm.someClickHandler = someClickHandler;
		vm.selected={};
		vm.selectAll=false;
		vm.toggleAll=toggleAll;
		vm.toggleOne=toggleOne;
		vm.dtInstance={};
		var titleHtml='<input type="checkbox" ng-model="showCase.selectAll" ng-click="showCase.toggleAll(showCase.selectAll,showCase.selected)">';
		vm.dtOptions=DTOptionsBuilder.newOptions().withOption('ajax',{
			type:'POST',
			url:'../mall/goodscomment/findByPage',
			dataSrc:'data'
		})
		.withOption('processing',true)
		.withOption('paging',true)
		.withOption('serverSide',true)
		.withOption('createdRow',function(row,data,dataIndex){
			$compile(angular.element(row).contents())($scope);
		})
		.withOption('headerCallback', function(header) {
            if (!$scope.headerCompiled) {
                $scope.headerCompiled = true;
                $compile(angular.element(header).contents())($scope);
            }
        })
        .withOption('stateSave', true)
        .withOption('rowCallback',rowCallback)
		.withPaginationType('full_numbers')
		.withLanguageSource('./app/language/chinese.json')
		
		$scope.$on('handleRequest',function(){
			
		});
		vm.dtColumns=[
		              DTColumnBuilder.newColumn(null).withTitle(titleHtml).notSortable().renderWith(function(data,type,full,meta){
		            	  vm.selected[full.id]=false;
		            	  return '<input type="checkbox" ng-model="showCase.selected['+data.id+']" ng-click="showCase.toggleOne(showCase.selected)">';
		              }),
		              DTColumnBuilder.newColumn('id').withTitle('ID').notVisible(),
			          DTColumnBuilder.newColumn('title').withTitle('标题').notSortable(),
			          DTColumnBuilder.newColumn('commentcontent').withTitle('评论内容').notSortable(),
			          DTColumnBuilder.newColumn('goodsname').withTitle('商品名称').notSortable(),
				      DTColumnBuilder.newColumn('relpyorcommentmembername').withTitle('评论人').notSortable(),
					  DTColumnBuilder.newColumn('virtualadd').withTitle('评论类型').notSortable(),
			          DTColumnBuilder.newColumn('reviewed').withTitle('审核').notSortable(),
			          DTColumnBuilder.newColumn('updatetime').withTitle('更新时间').notSortable(),
			          DTColumnBuilder.newColumn('status').withTitle('状态').notSortable(),
			          DTColumnBuilder.newColumn('version').withTitle('版本号').notSortable(),
			          DTColumnBuilder.newColumn(null).withTitle('操作').notSortable().renderWith(actionHtml)];
		function actionHtml(data,type,full,meta){
			return '<button class="btn btn-warning" ng-click="commentdetail('+data.id+')"><i class="fa fa-commenting-o"></i></button>';
		}

		function reloadData(){
			var resetPaging = false;
			vm.dtInstance.reloadData(callback, resetPaging);
		}
		function callback(data){
		}

		/**
		 * 复杂搜索刷新数据
		 */
		function searchReloadData(){
			var resetPaging=false;
			var searchParams={
				isCustomSearch:1,
				name:''
			};
			if($scope.commentcontent!=undefined){
				searchParams.name=$scope.commentcontent;
				vm.dtOptions['ajax'].data=searchParams;
				vm.dtInstance.reloadData(callback, resetPaging);
			}
		}

		/**
		 * 表格
		 */
		$scope.searchdataTableCustom=function(){
			searchReloadData();
		}

		/**
		 * 查询评论的详细信息包涵回复
		 * @param id
		 */
		$scope.commentdetail=function(id){
			$location.path('/goodscommentreplyment').search({'operate':'find','commentid':id});
		}
		
		///**
		// * 跳转到添加商品评论页面
		// */
		//$scope.save=function(){
		//	$location.path('/goodscomment').search({'operate':'save'});
		//}
		
		$scope.del=function(){
			var i=0;
			var ids=[];
			angular.forEach(vm.selected, function(data,index,array){
				if(data){
					i++;
					ids.push(index);
				}
			});
			if(i==0){
				$scope.errors={
						show:true,
						msg:'请选择一条记录'
				}
			}else{
				$scope.errors={
						show:false
				}
				//批量删除数据
				var idstrs=ids.join(",");
				$http({
					method:'POST',
					url:'../mall/goodscomment/del',
					data:{
						'commentid':idstrs
					}
				}).
				success(function(data,status,headers,config){
					if(data.sucflag){
						$scope.info={
								show:true,
								msg:'批量删除成功'
						}
						reloadData();
					}
					
				}).
				error(function(data,status,headers,config){
					$scope.errors={
							show:true,
							msg:'系统异常'
					}
				});
			}
		}

		/**
		 * 列表全选
		 */
		function toggleAll(selectAll,selectedItems){
			for(var id in selectedItems){
				if(selectedItems.hasOwnProperty(id)){
					selectedItems[id]=selectAll;
				}
			}
		}
		/**
		 * 列表单选
		 */
		function toggleOne(selectedItems){
			var me=this;
			for(var id in selectedItems){
				if(selectedItems.hasOwnProperty(id)){
					if(!selectedItems[id]){
						me.selectAll=false;
					}
				}
			}
			me.selectAll=true;
		}

		function someClickHandler(info) {
	        vm.message = info.id + ' - ' + info.name;
	    }
		/**
		 * 单击列表某行回调
		 */
		function rowCallback(nRow,aData,iDisplayIndex,iDisplayIndexFull){
			$('td', nRow).unbind('click');
	        $('td', nRow).bind('click', function() {
	            $scope.$apply(function() {
	                vm.someClickHandler(aData);
	            });
	        });
	        return nRow;
		}
	}
	/*=====End Of Find List GoodsCommentT=====*/



	goodscommentmodule.controller('goodscommentonement',ServerGoodsCommentOneListCtrl);
	//查询列表数据
	function ServerGoodsCommentOneListCtrl($http,$location,$compile,$scope,$rootScope,$resource,DTOptionsBuilder,DTColumnBuilder,DTAjaxRenderer){

		var goodsname = $location.search().goodsname;
		var goodsid=$location.search().id;
		$scope.title='正在查看商品--' + goodsname + '--的评论列表';
		var vm=this;
		vm.message='';
		vm.someClickHandler = someClickHandler;
		vm.selected={};
		vm.selectAll=false;
		vm.toggleAll=toggleAll;
		vm.toggleOne=toggleOne;
		vm.dtInstance={};
		var titleHtml='<input type="checkbox" ng-model="showCase.selectAll" ng-click="showCase.toggleAll(showCase.selectAll,showCase.selected)">';
		vm.dtOptions=DTOptionsBuilder.newOptions().withOption('ajax',{
				type:'POST',
				url:'../mall/goodscomment/findCommentByGoodsIdByPage',
				data:{
					goodsid:goodsid
				},
				dataSrc:'data'
			})
			.withOption('processing',true)
			.withOption('paging',true)
			.withOption('serverSide',true)
			.withOption('createdRow',function(row,data,dataIndex){
				$compile(angular.element(row).contents())($scope);
			})
			.withOption('headerCallback', function(header) {
				if (!$scope.headerCompiled) {
					$scope.headerCompiled = true;
					$compile(angular.element(header).contents())($scope);
				}
			})
			.withOption('stateSave', true)
			.withOption('rowCallback',rowCallback)
			.withPaginationType('full_numbers')
			.withLanguageSource('./app/language/chinese.json')

		$scope.$on('handleRequest',function(){

		});
		vm.dtColumns=[
			DTColumnBuilder.newColumn(null).withTitle(titleHtml).notSortable().renderWith(function(data,type,full,meta){
				vm.selected[full.id]=false;
				return '<input type="checkbox" ng-model="showCase.selected['+data.id+']" ng-click="showCase.toggleOne(showCase.selected)">';
			}),
			DTColumnBuilder.newColumn('id').withTitle('ID').notVisible(),
			DTColumnBuilder.newColumn('title').withTitle('标题').notSortable(),
			DTColumnBuilder.newColumn('commentcontent').withTitle('评论内容').notSortable(),
			DTColumnBuilder.newColumn('relpyorcommentmembername').withTitle('评论人').notSortable(),
			DTColumnBuilder.newColumn('virtualadd').withTitle('评论类型').notSortable(),
			DTColumnBuilder.newColumn('reviewed').withTitle('审核').notSortable(),
			DTColumnBuilder.newColumn('updatetime').withTitle('更新时间').notSortable(),
			DTColumnBuilder.newColumn('status').withTitle('状态').notSortable(),
			DTColumnBuilder.newColumn('version').withTitle('版本号').notSortable(),
			DTColumnBuilder.newColumn(null).withTitle('操作').notSortable().renderWith(actionHtml)];
		function actionHtml(data,type,full,meta){
			return '<button class="btn btn-warning" ng-click="commentdetail('+data.id+')"><i class="fa fa-commenting-o"></i></button>';
		}

		function reloadData(){
			var resetPaging = false;
			vm.dtInstance.reloadData(callback, resetPaging);
		}
		function callback(data){
		}

		/**
		 * 复杂搜索刷新数据
		 */
		function searchReloadData(){
			var resetPaging=false;
			var searchParams={
				isCustomSearch:1,
				name:''
			};
			if($scope.commentcontent!=undefined){
				searchParams.name=$scope.commentcontent;
				vm.dtOptions['ajax'].data=searchParams;
				vm.dtInstance.reloadData(callback, resetPaging);
			}
		}

		/**
		 * 表格
		 */
		$scope.searchdataTableCustom=function(){
			searchReloadData();
		}

		/**
		 * 查询评论的详细信息包涵回复
		 * @param id
         */
		$scope.commentdetail=function(id){
			$location.path('/goodscommentreplyment').search({'operate':'find','commentid':id});
		}

		/**
		 * 跳转到添加商品评论页面
		 */
		$scope.save=function(){
			var goodsid=$location.search().id;
			$location.path('/goodscomment').search({'operate':'save','goodsid':goodsid,'goodsname':goodsname});
		}

		$scope.del=function(){
			var i=0;
			var ids=[];
			angular.forEach(vm.selected, function(data,index,array){
				if(data){
					i++;
					ids.push(index);
				}
			});
			if(i==0){
				$scope.errors={
					show:true,
					msg:'请选择一条记录'
				}
			}else{
				$scope.errors={
					show:false
				}
				//批量删除数据
				var idstrs=ids.join(",");
				$http({
					method:'POST',
					url:'../mall/goodscomment/del',
					data:{
						'commentid':idstrs
					}
				}).
				success(function(data,status,headers,config){
					if(data.sucflag){
						$scope.info={
							show:true,
							msg:'批量删除成功'
						}
						reloadData();
					}

				}).
				error(function(data,status,headers,config){
					$scope.errors={
						show:true,
						msg:'系统异常'
					}
				});
			}
		}

		/**
		 * 列表全选
		 */
		function toggleAll(selectAll,selectedItems){
			for(var id in selectedItems){
				if(selectedItems.hasOwnProperty(id)){
					selectedItems[id]=selectAll;
				}
			}
		}
		/**
		 * 列表单选
		 */
		function toggleOne(selectedItems){
			var me=this;
			for(var id in selectedItems){
				if(selectedItems.hasOwnProperty(id)){
					if(!selectedItems[id]){
						me.selectAll=false;
					}
				}
			}
			me.selectAll=true;
		}

		function someClickHandler(info) {
			vm.message = info.id + ' - ' + info.name;
		}
		/**
		 * 单击列表某行回调
		 */
		function rowCallback(nRow,aData,iDisplayIndex,iDisplayIndexFull){
			$('td', nRow).unbind('click');
			$('td', nRow).bind('click', function() {
				$scope.$apply(function() {
					vm.someClickHandler(aData);
				});
			});
			return nRow;
		}
	}

});
